package com.qeado.report.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import jofc2.model.Chart;
import jofc2.model.Text;
import jofc2.model.axis.Label;
import jofc2.model.axis.XAxis;
import jofc2.model.axis.YAxis;
import jofc2.model.elements.LineChart;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.qeado.report.dao.ReportDao;
import com.qeado.report.service.ReportService;

@Service
public class ReportServiceImpl implements ReportService {
	@Autowired
	private ReportDao reportDao;

	@Override
	public String createUserCountReport(String timeFrom, String timeTo) {
        Map<String, String> map = new HashMap<String, String>();
        map.put("timeFrom", timeFrom);
        map.put("timeTo", timeTo);
		return this.createChart(reportDao.createUserCountReport(map), "用户注册量统计报表", "用户量");
	}

	@Override
	public String createBaikeVisitReport(String timeFrom, String timeTo) {
        Map<String, String> map = new HashMap<String, String>();
        map.put("timeFrom", timeFrom);
        map.put("timeTo", timeTo);
        return this.createChart(reportDao.createBaikeVisitReport(map), "百科访问量统计报表", "百科访问量");
	}

	@Override
	public String createProdPubReport(String timeFrom, String timeTo) {
        Map<String, String> map = new HashMap<String, String>();
        map.put("timeFrom", timeFrom);
        map.put("timeTo", timeTo);
        return this.createChart(reportDao.createProdPubReport(map), "商品发布量统计报表", "商品发布量");
	}

	@Override
	public String createSiteVisitReport(String timeFrom, String timeTo) {
        Map<String, String> map = new HashMap<String, String>();
        map.put("timeFrom", timeFrom);
        map.put("timeTo", timeTo);
        return this.createChart(reportDao.createSiteVisitReport(map), "网站访问量统计报表", "网站访问量");
	}
	
	protected String createChart(List<Map<String, String>> dataList, String title, String lineTitle) {
		Chart chart = new Chart();
		Text textTitle = new Text(title, Text.createStyle(20, "#FF7F00", Text.TEXT_ALIGN_CENTER));
        chart.setTitle(textTitle);
        //颜色对照表可见http://www.114la.com/other/rgb.htm
        chart.setBackgroundColour("#D1EEEE");//报表整体背景颜色
        
        if(dataList != null && dataList.size() > 0) {
            //y轴数据集合  
            List<Number> dataSet = new ArrayList<Number>();  
            //x轴数据集合
            List<Label> xLabel = new ArrayList<Label>(); 
            int max = 0,theNumber,i = 0;
            Label label ;
            double steps = Math.round(dataList.size()/8.0);
	        if(steps == 0)
	        	steps = 1;
        	for(Map<String, String> tempMap:dataList) {
        
	        	theNumber = Integer.valueOf(tempMap.get("theNumber"));
	        	if(theNumber > max)
	        		max = theNumber;
	        	//填充y轴  
	            dataSet.add(theNumber);  
	            //填充x轴  
	            label = new Label(tempMap.get("theDate"));
	            label.setRotation(Label.Rotation.DIAGONAL);
	            label.setVisible(i++%steps == 0);//设置x轴标签数据多久显示一个，此处和步长保持一致
	            xLabel.add(label); 
	        }
	        //设置X轴内容  
	        XAxis xAxis = new XAxis();  
	        xAxis.addLabels(xLabel); 
	        xAxis.setSteps(steps);
	        xAxis.setColour("#4F94CD");//x轴颜色
	        xAxis.setGridColour("#D1EEEE");//x轴网格颜色，此处设置跟背景色一样，不显示网格
	        //设置Y轴显示值域:Range的三个参数含义为：坐标最小值，最大值和步进值  
	        YAxis yAxis = new YAxis();  
	        steps = Math.round(max/10.0);
	        if(steps == 0)
	        	steps = 1;
	        yAxis.setRange(0, max, steps);  
	        yAxis.setColour("#4F94CD");//y轴颜色
	        yAxis.setGridColour("#D1EEEE");//y轴网格颜色，此处设置跟背景色一样，不显示网格
	        //OFC折线图设置  
	        LineChart lineChart = new LineChart(LineChart.Style.HOLLOW);  
	        lineChart.addValues(dataSet);  
	        lineChart.setColour("#BA55D3");//数据线颜色  
	        lineChart.setText(lineTitle);  
	        lineChart.setDotSize(4);
	        //图表设置  
	        chart.setXAxis(xAxis);  
	        chart.setYAxis(yAxis);  
	        //lineChart.setTooltip("#val#<br>#x_label#");
	        chart.addElements(lineChart);
        }
		return chart.toString();
	}

}
